package com.reden;

import java.awt.*;
import java.awt.event.InputEvent;
import java.util.TimerTask;

public class Automatic extends TimerTask {
    private Integer tick;
    private Integer period;
    private Boolean left;
    private Boolean right;
    private Boolean enable;
    private Robot robot;

    public Automatic(Integer period, Boolean left, Boolean right) {
        try {
            this.robot = new Robot();
        } catch (AWTException e) {
            e.printStackTrace();
        }
        this.tick = 1;
        this.period = period;
        this.left = left;
        this.right = right;
        this.enable = false;
        new Restrain(this);
        new KeyListener(this);

    }

    public Integer getPeriod() {
        return period;
    }

    public void setPeriod(Integer period) {
        this.period = period;
    }

    public Boolean getLeft() {
        return left;
    }

    public void setLeft(Boolean left) {
        this.left = left;
    }

    public Boolean getRight() {
        return right;
    }

    public void setRight(Boolean right) {
        this.right = right;
    }

    public Boolean getEnable() {
        return enable;
    }

    public void setEnable(Boolean enable) {
        this.enable = enable;
    }

    @Override
    public void run() {
        tick++;
        if (enable && tick > period) {
            tick = 1;
            if (left) {
                robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);
                robot.delay(1);
                robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);
            }
            if (right) {
                robot.mousePress(InputEvent.BUTTON3_DOWN_MASK);
                robot.delay(1);
                robot.mouseRelease(InputEvent.BUTTON3_DOWN_MASK);
            }
        }
    }
}
